package com.cykj.faceledtv.netty;

import com.cykj.faceledtv.BuildConfig;

import java.util.concurrent.TimeUnit;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @author jzh
 * @date 2018/11/06 11:05
 */
public class NettyClientInitializer extends ChannelInitializer<SocketChannel> {

    private NettyListener listener;


    public NettyClientInitializer(NettyListener listener) {
        this.listener = listener;
    }

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
//        pipeline.addLast(sslCtx.newHandler(ch.alloc()));    // 开启SSL
        // 开启日志，可以设置日志等级
        if (BuildConfig.DEBUG) {
            pipeline.addLast(new LoggingHandler(LogLevel.INFO));
        }
        //编码解码
        pipeline.addLast(new NettyDecode());
        pipeline.addLast(new NettyEncode());
//        心跳管理 读写心跳超时时间
        pipeline.addLast(new IdleStateHandler(100, 110, 120, TimeUnit.SECONDS));
//        pipeline.addLast(new IdleStateHandler(30, 60, 100));
        pipeline.addLast(new NettyClientHandler(listener));
    }
}
